import { debounce } from "./common"

/**
 * 显示toast
 * @param content 内容
 */
export const showToast = (content: string) => {
  const toastDiv = document.getElementsByClassName('toast')
  if (toastDiv.length === 0) {
    const div = document.createElement('div')
    div.className = 'toast'
    div.style.cssText = 'position: fixed; z-index: 9; top: 96px; left: 50%; margin: auto; transform: translateX(-50%); display: inline-block; padding: 12px 24px; border-radius: 6px; background: rgb(0, 0, 0, 0.6); color: #fff;'
    div.innerText = content
    document.body.append(div)
  } else {
    (toastDiv[0] as HTMLDivElement).innerText = content
  }
  removeToast()
}

/**
 * 移除Toast
 */
const removeToast = debounce(() => {
  const toastDiv = document.getElementsByClassName('toast')
  toastDiv.length > 0 && document.body.removeChild(toastDiv[0])
}, 2000)
